/*
* Copyright: JessMA Open Source (ldcsaa@gmail.com)
*
* Version : 3.2.3
* Author : Bruce Liang
* Website : http://www.jessma.org
* Project : https://github.com/ldcsaa
* Blog : http://www.cnblogs.com/ldcsaa
* Wiki : http://www.oschina.net/p/hp-socket
* QQ Group : 75375912
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jessma.hpsocket.mbcs;
import org.jessma.hpsocket.Buffer;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
/** TCP Agent 组件基类 (MBCS) */
public class TcpAgent extends Agent
{
/** 创建组件对象 */
public static TcpAgent create(Mode mode)
{
TcpAgent instance = new TcpAgent();
instance.mode = mode;
switch(mode)
{
case PUSH:
instance.socketListener = HPSocket.SDK.Create_HP_TcpAgentListener();
instance.socketObj = HPSocket.SDK.Create_HP_TcpAgent(instance.socketListener);
break;
case PULL:
instance.socketListener = HPSocket.SDK.Create_HP_TcpPullAgentListener();
instance.socketObj = HPSocket.SDK.Create_HP_TcpPullAgent(instance.socketListener);
break;
default:
assert false;
}
return instance;
}
/** 销毁组件对象 */
public static void destroy(TcpAgent agent)
{
if(agent.socketObj == null && agent.socketListener == null)
return;
synchronized(agent)
{
switch(agent.mode)
{
case PUSH:
if(agent.socketObj != null)
HPSocket.SDK.Destroy_HP_TcpAgent(agent.socketObj);
if(agent.socketListener != null)
HPSocket.SDK.Destroy_HP_TcpAgentListener(agent.socketListener);
break;
case PULL:
if(agent.socketObj != null)
HPSocket.SDK.Destroy_HP_TcpPullAgent(agent.socketObj);
if(agent.socketListener != null)
HPSocket.SDK.Destroy_HP_TcpPullAgentListener(agent.socketListener);
break;
default:
assert false;
}
agent.clearReferences();
}
}
protected TcpAgent()
{
}
@Override
protected void finalize() throws Throwable
{
destroy(this);
super.finalize();
}
/* ********************************* TCP Agent 组件操作方法 ****************************** */
/** 发送小文件 */
public boolean sendSmallFile(NativeLong dwConnID, String lpszFileName, byte[] pHead, byte[] pTail)
{
Buffer headBuffer = pHead != null ? new Buffer(pHead) : null;
Buffer tailBuffer = pTail != null ? new Buffer(pTail) : null;
return sendSmallFile(dwConnID, lpszFileName, headBuffer, tailBuffer);
}
/** 发送小文件 */
public boolean sendSmallFile(NativeLong dwConnID, String lpszFileName, Buffer pHead, Buffer pTail)
{
long pMemHead = 0;
long pMemTail = 0;
Pointer pHeadBuffer = null;
Pointer pTailBuffer = null;
if(pHead != null)
{
pMemHead = Native.malloc(Buffer.NATIVE_BUFFER_SIZE);
pHeadBuffer = new Pointer(pMemHead);
pHeadBuffer.setInt(0, pHead.getLength());
pHeadBuffer.setPointer(Pointer.SIZE, pHead.getPointer());
}
if(pTail != null)
{
pMemTail = Native.malloc(Buffer.NATIVE_BUFFER_SIZE);
pTailBuffer = new Pointer(pMemTail);
pTailBuffer.setInt(0, pTail.getLength());
pTailBuffer.setPointer(Pointer.SIZE, pTail.getPointer());
}
boolean result = HPSocket.SDK.HP_TcpAgent_SendSmallFile(socketObj, dwConnID, str2MbcsBytes(lpszFileName), pHeadBuffer, pTailBuffer);
if(pMemHead != 0) Native.free(pMemHead);
if(pMemTail != 0) Native.free(pMemTail);
return result;
}
/* ******************************** TCP Agent 属性访问方法 ******************************* */
/** 设置是否启用地址重用机制(默认:不启用) */
public void setReuseAddress(boolean bReuseAddress)
{
HPSocket.SDK.HP_TcpAgent_SetReuseAddress(socketObj, bReuseAddress);
}
/** 检测是否启用地址重用机制 */
public boolean isReuseAddress()
{
return HPSocket.SDK.HP_TcpAgent_IsReuseAddress(socketObj);
}
/** 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为 1024 的倍数) */
public void setSocketBufferSize(int dwSocketBufferSize)
{
HPSocket.SDK.HP_TcpAgent_SetSocketBufferSize(socketObj, dwSocketBufferSize);
}
/** 设置心跳包间隔(毫秒,0 则不发送心跳包) */
public void setKeepAliveTime(int dwKeepAliveTime)
{
HPSocket.SDK.HP_TcpAgent_SetKeepAliveTime(socketObj, dwKeepAliveTime);
}
/** 设置心跳确认包检测间隔(毫秒,0 不发送心跳包,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线) */
public void setKeepAliveInterval(int dwKeepAliveInterval)
{
HPSocket.SDK.HP_TcpAgent_SetKeepAliveInterval(socketObj, dwKeepAliveInterval);
}
/** 获取通信数据缓冲区大小 */
public int getSocketBufferSize()
{
return HPSocket.SDK.HP_TcpAgent_GetSocketBufferSize(socketObj);
}
/** 获取心跳检查次数 */
public int getKeepAliveTime()
{
return HPSocket.SDK.HP_TcpAgent_GetKeepAliveTime(socketObj);
}
/** 获取心跳检查间隔 */
public int getKeepAliveInterval()
{
return HPSocket.SDK.HP_TcpAgent_GetKeepAliveInterval(socketObj);
}
/* ***************************** TCP Pull Agent 组件操作方法 **************************** */
/**
* 抓取数据
*/
public int fetch(NativeLong dwConnID, byte[] pBuffer, int iLength)
{
return HPSocket.SDK.HP_TcpPullAgent_Fetch(socketObj, dwConnID, pBuffer, iLength);
}
}